﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using DBService;

namespace YJKArch.RoomRoofModule
{
	public partial class DlgRoomInfo : System.Windows.Forms.Form
	{
		public static DlgRoomInfo GetInstance(ExternalCommandData rvt, bool service, string fileName)
		{
			if (DlgRoomInfo.dri == null || DlgRoomInfo.dri.IsDisposed)
			{
				DlgRoomInfo.dri = new DlgRoomInfo(rvt, service, fileName);
			}
			return DlgRoomInfo.dri;
		}

		public DlgRoomInfo(ExternalCommandData rvt, bool service, string fileName)
		{
			this.IsService = service;
			this.FileName = fileName;
			this.revit = rvt;
			this.uiDoc = this.revit.Application.ActiveUIDocument;
			this.doc = this.uiDoc.Document;
			this.InitializeComponent();
			if (service)
			{
				this.rbByFile.Checked = false;
				this.rbByServer.Checked = true;
				this.tbFileName.Visible = false;
				return;
			}
			this.rbByFile.Checked = true;
			this.rbByServer.Checked = false;
			this.tbFileName.Visible = true;
			this.tbFileName.Text = fileName;
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
		}

		private void propertyGrid1_Click(object sender, EventArgs e)
		{
		}

		private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				this.btnCommit.Enabled = true;
				this.propertyGrid1.Enabled = true;
				DbRoomInfo dbRoomInfo = (DbRoomInfo)this.treeView1.SelectedNode.Tag;
				Element element = RevitVersionFuncs.GetElement(this.doc, new ElementId(dbRoomInfo.RoomId));
				this.uiDoc.ShowElements(element);
				this.propertyGrid1.SelectedObject = dbRoomInfo;
			}
		}

		private void btnCommit_Click(object sender, EventArgs e)
		{
			if (this.rbByFile.Checked)
			{
				IEnumerator enumerator = this.treeView1.Nodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						((DbRoomInfo)((TreeNode)obj).Tag).UpdateToXML(this.FileName);
					}
					goto IL_A3;
				}
			}
			foreach (object obj2 in this.treeView1.Nodes)
			{
				((DbRoomInfo)((TreeNode)obj2).Tag).UpdateToDb();
			}
			IL_A3:
			MessageBox.Show("提交完成。", "盈建科提示");
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnView_Click(object sender, EventArgs e)
		{
			if (this.treeView1.SelectedNode != null)
			{
				DbRoomInfo dbRoomInfo = (DbRoomInfo)this.treeView1.SelectedNode.Tag;
				Element element = RevitVersionFuncs.GetElement(this.doc, new ElementId(dbRoomInfo.RoomId));
				this.uiDoc.ShowElements(element);
			}
		}

		private void DlgRoomInfo_Load(object sender, EventArgs e)
		{
			this.treeView1.Nodes.Clear();
			if (this.IsService)
			{
				DbProject projectByCurrentDoc = DbProject.GetProjectByCurrentDoc(this.doc);
				if (projectByCurrentDoc == null)
				{
					MessageBox.Show("此模型未加入任何项目，请先创建项目再操作。", "盈建科提示");
					base.Close();
					return;
				}
				new List<Room>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
				filteredElementCollector.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_Rooms);
				foreach (Element element in filteredElementCollector)
				{
					Room room = element as Room;
					if (room != null && room.Location != null)
					{
						DbRoomInfo dbRoomInfo;
						if (DbRoomInfo.GetByProject(projectByCurrentDoc, room.Id.IntegerValue) == null)
						{
							dbRoomInfo = new DbRoomInfo(projectByCurrentDoc.Id, room);
							dbRoomInfo.AddToDb(BimServerDB.Default);
						}
						else
						{
							dbRoomInfo = DbRoomInfo.GetByProject(projectByCurrentDoc, room.Id.IntegerValue);
						}
						TreeNode treeNode = new TreeNode(dbRoomInfo.Name);
						treeNode.Tag = dbRoomInfo;
						this.treeView1.Nodes.Add(treeNode);
						dbRoomInfo = new DbRoomInfo(projectByCurrentDoc.Id, room);
						dbRoomInfo.AddToDb(BimServerDB.Default);
					}
				}
				List<DbRoomInfo> byProject = DbRoomInfo.GetByProject(projectByCurrentDoc);
				List<ElementId> list = filteredElementCollector.ToElementIds() as List<ElementId>;
				foreach (DbRoomInfo dbRoomInfo2 in byProject)
				{
					ElementId item = new ElementId(dbRoomInfo2.RoomId);
					if (!list.Contains(item))
					{
						dbRoomInfo2.RemoveFromDb();
					}
				}
				if (this.treeView1.Nodes.Count <= 0)
				{
					MessageBox.Show("该项目目前没有房间信息！", "盈建科提示");
					base.Close();
					return;
				}
			}
			else
			{
				new List<Room>();
				FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.doc);
				filteredElementCollector2.OfClass(typeof(SpatialElement)).OfCategory(BuiltInCategory.OST_Rooms);
				foreach (Element element2 in filteredElementCollector2)
				{
					Room room2 = element2 as Room;
					if (room2 != null && room2.Location != null)
					{
						DbRoomInfo dbRoomInfo3 = null;
						bool flag = false;
						foreach (DbRoomInfo dbRoomInfo4 in DbRoomInfo.ReadFromXML(this.FileName))
						{
							if (dbRoomInfo4.RoomId == room2.Id.IntegerValue)
							{
								dbRoomInfo3 = dbRoomInfo4;
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							dbRoomInfo3 = new DbRoomInfo(0, room2);
							if (!dbRoomInfo3.WirteToXML(this.FileName))
							{
								base.Close();
								return;
							}
						}
						TreeNode treeNode2 = new TreeNode(dbRoomInfo3.Name);
						treeNode2.Tag = dbRoomInfo3;
						this.treeView1.Nodes.Add(treeNode2);
					}
				}
				List<DbRoomInfo> list2 = DbRoomInfo.ReadFromXML(this.FileName);
				List<ElementId> list3 = filteredElementCollector2.ToElementIds() as List<ElementId>;
				foreach (DbRoomInfo dbRoomInfo5 in list2)
				{
					ElementId item2 = new ElementId(dbRoomInfo5.RoomId);
					if (!list3.Contains(item2))
					{
						dbRoomInfo5.RemoveFromXml(this.FileName);
					}
				}
				if (this.treeView1.Nodes.Count <= 0)
				{
					MessageBox.Show("该项目目前没有房间信息！", "盈建科提示");
					base.Close();
				}
			}
		}

		private void propertyGrid1_Validating(object sender, CancelEventArgs e)
		{
		}

		private void propertyGrid1_Validated(object sender, EventArgs e)
		{
		}

		private static DlgRoomInfo dri;

		private ExternalCommandData revit;

		private Document doc;

		private UIDocument uiDoc;

		public bool IsService;

		public string FileName;
	}
}
